﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Text;
using EmoneyWeiXinBLL;
using EmoneyWeiXinEntity;
using System.IO;
using System.Data;
using System.Text.RegularExpressions;
using EmoneyWeiXinCommon;
using EmoneyWeiXin.Filter;

namespace EmoneyWeiXin.Controllers
{
    public class CreateReportByModelController : Controller
    {
        //
        // GET: /CreateReportByModel/
        [LoginAuthorizeAttribute]
        public ActionResult Index()
        {
            
            //CreateNeiMuchuanwen();
            //CreateDaShiJian();
            //CreateGeGuYanBao();
            //CreateNeiZhongDaLihao();
            return View();
        }

        public ActionResult DoGet()
        {
            string type = Request["type"];
            if (type == "neimu")
            {
                return Content(CreateNeiMuchuanwen());
            }
            if (type == "lihao")
            {
                return Content(CreateNeiZhongDaLihao());
            }
            if (type == "yanbao")
            {
                return Content(CreateGeGuYanBao());
            }
            if (type == "xinwen")
            {
                return Content(CreateDaShiJian());
            }
            else return Content("");
        }
        /// <summary>
        /// 大事件
        /// </summary>
        /// <returns></returns>
        private string CreateDaShiJian()
        {

            DataTable data = CreateReportByModelBLL.GetDaShiJianData();
            StringBuilder sbSum = new StringBuilder();
            #region 总模板
            sbSum.Append(@"<section class='tn-Powered-by-XIUMI' style='font-size: 1em; white-space: normal; border: 0px; margin: 0px; line-height: 1.4; text-align: justify; box-sizing: border-box; padding: 0px;'>
                        <section class='tn-Powered-by-XIUMI' style='padding: 0px 8px; border-left-width: 3px; border-left-style: solid; border-color: rgb(249, 94, 87); font-size: 1.5em; font-family: inherit; text-decoration: inherit; color: rgb(9, 9, 9); box-sizing: border-box;'>
                        <section class='tn-Powered-by-XIUMI' style='line-height: 1.4; font-family: inherit; box-sizing: border-box;'>
                        <section class='tn-Powered-by-XIUMI' style='box-sizing: border-box;'>
                        <strong>大事件导读</strong>
                        </section>
                        </section>
                        <section class='tn-Powered-by-XIUMI' style='line-height: 1.4; margin-top: 5px; padding-left: 3px; font-size: 14px; font-family: inherit; text-decoration: inherit; color: rgb(51, 51, 51); box-sizing: border-box;'>
                       {[temp0001]}
                       </section>
                        </section>
                        <section style='width: 0px; height: 0px; clear: both;'></section>
                        </section>
                        <p style='clear: both; white-space: normal;'>
                        <br />
                         </p>
                         <p>
                        {[temp0002]}     
                        </p>
                        <p style='clear: both; white-space: normal;'>
                        <br />
                        </p>
                         <p>
                        <br />
                       </p>
                        ");
            #endregion
            #region tempStr1
            string tempStr1 = @"<section class='tn-Powered-by-XIUMI' style='box-sizing: border-box;'>
                                 {0}<br class='tn-Powered-by-XIUMI' style='box-sizing: border-box;'/>
                               </section>";
            #endregion
            #region tempStr2
            string tempStr2 = @" <section class='tn-Powered-by-XIUMI' style='color: inherit; font-family: inherit; white-space: normal; border: 0px; margin-top: 0px; margin-bottom: 0px; clear: both; font-size: 18px; font-weight: bold; text-align: justify; text-decoration: inherit; box-sizing: border-box; padding: 0px;'>
        <section class='tn-Powered-by-XIUMI' style='box-sizing: border-box;'>
            {0}
        </section>
        <section style='width: 0px; height: 0px; clear: both;'></section>
    </section>
    <section class='tn-Powered-by-XIUMI' style='white-space: normal; border: 0px; margin: 0.5em 0px; padding: 0.3em; box-sizing: border-box;'>
        <section class='tn-Powered-by-XIUMI' style='border: 1px solid rgb(192, 200, 209); padding: 10px; box-shadow: rgb(170, 170, 170) 0px 0px 10px; font-size: 1em; line-height: 1.4; color: rgb(51, 51, 51); font-family: inherit; text-decoration: inherit; box-sizing: border-box; background-color: rgb(255, 255, 255);'>
            <section class='tn-Powered-by-XIUMI' style='box-sizing: border-box;'>
               {1}<br class='tn-Powered-by-XIUMI' style='box-sizing: border-box;' />
            </section>
            <section class='tn-Powered-by-XIUMI' style='box-sizing: border-box;'>
                <br class='tn-Powered-by-XIUMI' style='box-sizing: border-box;' />
            </section>
            <section class='tn-Powered-by-XIUMI' style='box-sizing: border-box;'>
                <strong><span style='color: rgb(84, 141, 212);'>点评：</span></strong><span style='color: rgb(84, 141, 212);'>{2}</span><br class='tn-Powered-by-XIUMI' style='box-sizing: border-box;' />
            </section>
            <section class='tn-Powered-by-XIUMI' style='box-sizing: border-box;'>
                <br class='tn-Powered-by-XIUMI' style='box-sizing: border-box;' />
            </section>
            <section class='tn-Powered-by-XIUMI' style='box-sizing: border-box;'>
                <span style='color: rgb(255, 0, 0);'>{3}</span>
            </section>
        </section>
        <section style='width: 0px; height: 0px; clear: both;'></section>
    </section>";
            #endregion
            StringBuilder sb1 = new StringBuilder();
            StringBuilder sb2 = new StringBuilder();
            foreach (DataRow dr in data.Rows)
            {
                string subTitle = dr["f006v_12001"].ToString();
                if (Convert.ToInt32(dr["f004v_12001"]) >= 4)
                {
                    subTitle = "★" + subTitle;
                }
                sb1.Append(string.Format(tempStr1, subTitle));
                sb2.Append(string.Format(tempStr2, subTitle, dr["f013v_12001"], ("点评:" + dr["f006v_12006"]), (dr["f023v_12001"] + "相关个股：" + dr["f021v_12001"])));
            }
            sbSum.Replace("{[temp0001]}", sb1.ToString());
            sbSum.Replace("{[temp0002]}", sb2.ToString());

            string title = "【早知道" + System.DateTime.Now.ToString("MMdd") + "】" + data.Rows[0]["f006v_12001"];
            SaveToDb("xinwen", title, data.Rows[0]["f013v_12001"].ToString(), sbSum.ToString(), "http://static.emoney.cn/webupload/JiaQunTwoCode/2015/11/9e09ef29-4ed9-47b5-96e3-5a503e59697e.jpg");
            return "1";
        }
        /// <summary>
        /// 个股研报
        /// </summary>
        private string CreateGeGuYanBao()
        {

            #region 总模板
            string sumSb = @"<section class='tn-Powered-by-XIUMI' style='font-size: 1em; white-space: normal; margin: 0.8em 0px; line-height: 1.4; box-sizing: border-box; padding: 0px; border: 0px;'>
    <section class='tn-Powered-by-XIUMI' style='padding: 10px; border: 2px dashed rgb(249, 94, 87); font-size: 1em; font-family: inherit; text-decoration: inherit; color: rgb(51, 51, 51); box-sizing: border-box;'>
        <section class='tn-Powered-by-XIUMI' style='box-sizing: border-box;'>
            <span style='font-size: 14px;'>{[template1]}</span>
        </section>
    </section>
    <section style='width: 0px; height: 0px; clear: both;'></section>
</section>
<p style='clear: both; white-space: normal;'>
    <br />
</p>
<table style='width: 100%; margin-bottom: 10px; border-collapse: collapse; color: inherit; font-family: inherit; white-space: normal; font-size: 18px; text-align: justify; text-decoration: inherit;'>
    <tr>
        <td style='border: 1px solid rgb(249, 110, 87); background-color:rgb(246, 156, 15)'>代码</td>
        <td style='border: 1px solid rgb(249, 110, 87); background-color:rgb(246, 156, 15)'>简称</td>
        <td style='border: 1px solid rgb(249, 110, 87); background-color: rgb(246, 156, 15)'>调研日期</td>
        <td style='border: 1px solid rgb(249, 110, 87); background-color: rgb(246, 156, 15)'>调研家数</td>
        <td style='border: 1px solid rgb(249, 110, 87); background-color: rgb(246, 156, 15)'>明星机构</td>
    </tr>
    {[template2]}
</table>
<br />
<p style='clear: both; white-space: normal;'>
    <br />
</p>
<section class='tn-Powered-by-XIUMI' style='white-space: normal; border: 0px; margin: 0.8em 0px 0.5em; overflow: hidden; box-sizing: border-box; padding: 0px;'>
    <section class='tn-Powered-by-XIUMI' style='display: inline-block; font-size: 1em; font-family: inherit; text-decoration: inherit; color: rgb(255, 255, 255); border-color: rgb(249, 110, 87); box-sizing: border-box;'>
        <section class='tn-Powered-by-XIUMI' style='height: 2em; display: inline-block; padding: 0.3em 0.5em; text-align: center; font-size: 1em; line-height: 1.4; vertical-align: top; font-family: inherit; box-sizing: border-box; background-color: rgb(249, 110, 87);'>
            <section class='tn-Powered-by-XIUMI' style='box-sizing: border-box;'>
                <span style='font-size: 14px;'>调研详情</span>
            </section>
        </section>
        <section class='tn-Powered-by-XIUMI' style='display: inline-block; height: 2em; width: 0.5em; vertical-align: top; border-left-width: 0.5em; border-left-style: solid; border-left-color: rgb(249, 110, 87); font-size: 1em; box-sizing: border-box; border-top-width: 1em !important; border-top-style: solid !important; border-top-color: transparent !important; border-bottom-width: 1em !important; border-bottom-style: solid !important; border-bottom-color: transparent !important;'></section>
    </section>
    <section style='width: 0px; height: 0px; clear: both;'></section>
</section>
 {[template3]}
";
            #endregion
            DataTable dt = CreateReportByModelBLL.GetJigouDiaoYan();
            string tempStr1 = "【机构调研" + System.DateTime.Now.ToString("MMdd") + "】{0}家上市公司被机构调研，其中{1}等{2}家受明星机构青睐";

            string tempStr2 = @"<tr>
        <td style='border: 1px solid rgb(249, 110, 87); '>{0}</td>
        <td style='border: 1px solid rgb(249, 110, 87); '>{1}</td>
        <td style='border: 1px solid rgb(249, 110, 87); '>{2}</td>
        <td style='border: 1px solid rgb(249, 110, 87); '>{3}</td>
        <td style='border: 1px solid rgb(249, 110, 87); '>{4}</td>
        </tr>";
            string tempStr3 = @"<p style='color: rgb(249, 110, 87);'>{0}</p>
                                <p style='line-height: 25px;color:#808080'>{1}</p>";
            string sqlgp = "";//记录受青睐的股票
            int sqlCount = 0;
            StringBuilder sb2 = new StringBuilder();
            StringBuilder sb3 = new StringBuilder();

            foreach (DataRow dr in dt.Rows)
            {
                if (Convert.ToInt32(dr["StarOrgNum"]) > 0) //获取受明星机构青睐的股票
                {
                    sqlgp += dr["SecuAbbr"] + "、";
                    sqlCount++;
                }

                sb2.Append(string.Format(tempStr2, dr["SecuCode"], dr["SecuAbbr"], Convert.ToDateTime(dr["ActionDate"]).ToString("MM/dd"), dr["ActOrgNum"], dr["StarOrgNum"]));
            }



            string wenda = dt.Rows[0]["Content"].ToString(); //只获取第一格股票的问答
            string[] t = wenda.Split('>');
            int idex = 0;
            foreach (string item in t)
            {
                if (item.Trim() != "")
                {

                    string[] t2 = item.Split('"');
                    idex++;

                    string wen = idex + "、" + t2[3];
                    string da = t2[5].Replace("&lt;","<").Replace("&gt;",">");
                    sb3.Append(string.Format(tempStr3, wen, da));
                }
            }

            tempStr1 = string.Format(tempStr1, dt.Rows.Count, sqlgp.TrimEnd('、'), sqlCount);
            string content = sumSb.Replace("{[template1]}", tempStr1).Replace("{[template2]}", sb2.ToString()).Replace("{[template3]}", sb3.ToString()); ;


            SaveToDb("diaoyan",
                "【机构调研" + System.DateTime.Now.ToString("MMdd") + "】" + "爆" + dt.Rows[0]["ActOrgNum"] + "家机构热捧" + dt.Rows[0]["SecuAbbr"]
                , tempStr1, content.ToString(), "http://static.emoney.cn/webupload/JiaQunTwoCode/2015/11/10973a3a-eca3-4c5b-8f7c-b94fee2324f3.jpg");
            return "1";
        }

        /// <summary>
        /// 涨停揭秘
        /// </summary>
        /// <returns></returns>
        private string CreateZhangtingjiemi()
        {
            return "1";
        }

        /// <summary>
        /// 内幕传闻
        /// </summary>
        /// <returns></returns>
        private string CreateNeiMuchuanwen()
        {
            DataTable dt = CreateReportByModelBLL.GetNeiMuChuanwen();
            StringBuilder content = new StringBuilder();
            string tempelate = "<p style=\"clear: both; text-align: justify; white-space: normal; box-sizing: border-box; line-height: 1.5em;\"><span style=\"color: rgb(84, 141, 212);\">{0}：</span>{1}</p>";
            foreach (DataRow dr in dt.Rows)
            {
                content.Append(string.Format(tempelate, dr["f021v_12001"], dr["f006v_12001"]));
            }
            //描述,最晚三条的股票加标题
            string summary = "";
            for (int i = 0; i < 3; i++)
            {
                summary += "" + dt.Rows[i]["f021v_12001"] + dt.Rows[i]["f006v_12001"] + ";";
            }

            SaveToDb("neimu", "【内幕传闻" + System.DateTime.Now.ToString("MMdd") + "】", summary, content.ToString(), "http://static.emoney.cn/webupload/JiaQunTwoCode/2015/11/81448dde-0811-438a-9f15-52df2d4b9753.jpg");
            return "1";
        }

        /// <summary>
        /// 重大利好
        /// </summary>
        /// <returns></returns>
        private string CreateNeiZhongDaLihao()
        {
            string[] star = { "", "★", "★★", "★★★", "★★★★", "★★★★★" };
            DataSet ds = CreateReportByModelBLL.GetZhongDaLiHao();
            DataTable tb1 = ds.Tables[0];
            DataTable tb2 = ds.Tables[1];

            #region sumStr
            string sumStr = @"<section class='tn-Powered-by-XIUMI' style='font-family: inherit; font-size: 1em; white-space: normal; margin: 1em; padding: 0px; border: 1px solid rgb(192, 200, 209); text-decoration: inherit; color: rgb(51, 51, 51); box-sizing: border-box; background-color: rgb(239, 239, 239);'>
    <section class='tn-Powered-by-XIUMI' style='margin: -1em 16px 0px; padding: 0px; border: none; line-height: 1.4; box-sizing: border-box;'>
        <span class='tn-Powered-by-XIUMI' style='display: inline-block; padding: 3px 8px; border-radius: 4px; color: rgb(255, 255, 255); font-size: 1em; text-align: center; font-family: inherit; text-decoration: inherit; border-color: rgb(238, 72, 72); box-sizing: border-box; background-color: rgb(238, 72, 72);'><section class='tn-Powered-by-XIUMI' style='box-sizing: border-box;'>
            <strong>重大利好</strong>
        </section></span>
    </section>
    <section class='tn-Powered-by-XIUMI' style='padding: 16px; font-size: 1em; line-height: 1.4; font-family: inherit; box-sizing: border-box;'>
        <section class='tn-Powered-by-XIUMI' style='box-sizing: border-box;'>
        <!--    389家上市公司披露740条公告，A股市场（A-stockmarket）从中筛选出14条，供您参考。-->
            {[template1]}
        </section>
    </section>
    <section style='width: 0px; height: 0px; clear: both;'></section>
</section>

<p>
   {[template2]}
</p>
";
            #endregion

            #region tempStr1
            string tempStr1 = @"  <section class='tn-Powered-by-XIUMI' style='white-space: normal; border: 0px; margin: 0.8em 0px 0.5em; overflow: hidden; padding: 0px; box-sizing: border-box !important;'>
        <section class='tn-Powered-by-XIUMI' style='display: inline-block; font-size: 1em; font-family: inherit; text-decoration: inherit; color: rgb(255, 255, 255); border-color: rgb(238, 72, 72); box-sizing: border-box;'>
            <section class='tn-Powered-by-XIUMI' style='height: 2em; display: inline-block; padding: 0.3em 0.5em; text-align: center; font-size: 1em; line-height: 1.4; vertical-align: top; font-family: inherit; box-sizing: border-box !important; background-color: rgb(238, 72, 72);'>
                <section class='tn-Powered-by-XIUMI' style='box-sizing: border-box;'>
                    <strong>{0}</strong>
                </section>
            </section>
            <section class='tn-Powered-by-XIUMI' style='display: inline-block; height: 2em; width: 0.5em; vertical-align: top; border-left-width: 0.5em; border-left-style: solid; border-left-color: rgb(238, 72, 72); font-size: 1em; box-sizing: border-box !important; border-top-width: 1em !important; border-top-style: solid !important; border-top-color: transparent !important; border-bottom-width: 1em !important; border-bottom-style: solid !important; border-bottom-color: transparent !important;'></section>
        </section>
        <section style='width: 0px; height: 0px; clear: both;'></section>
    </section>
    <section class='tn-Powered-by-XIUMI' style='color: inherit; font-family: inherit; font-size: 1em; white-space: normal; border: 0px; margin-top: 0px; margin-bottom: 0px; clear: both; text-align: justify; text-decoration: inherit; box-sizing: border-box; padding: 0px;'>
        <section class='tn-Powered-by-XIUMI' style='box-sizing: border-box;'>
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;利好指数：{1}
        </section>
        <section style='width: 0px; height: 0px; clear: both;'></section>
    </section>
    <section class='tn-Powered-by-XIUMI' style='font-size: 1em; white-space: normal; margin: 0.8em 0px; line-height: 1.4; box-sizing: border-box; padding: 0px; border: 0px;'>
        <section class='tn-Powered-by-XIUMI' style='padding: 10px; border: 1px solid rgb(192, 200, 209); font-size: 1em; font-family: inherit; text-decoration: inherit; color: rgb(51, 51, 51); box-sizing: border-box; background-color: rgb(239, 239, 239);'>
            <section class='tn-Powered-by-XIUMI' style='box-sizing: border-box;'>
                {2}
            </section>
        </section>
        <section style='width: 0px; height: 0px; clear: both;'></section>
    </section>
    <section class='tn-Powered-by-XIUMI' style='color: inherit; font-family: inherit; font-size: 1em; white-space: normal; border: 0px; margin-top: 0px; margin-bottom: 0px; clear: both; text-align: justify; text-decoration: inherit; box-sizing: border-box; padding: 0px;'>
        <section class='tn-Powered-by-XIUMI' style='box-sizing: border-box;'>
            <br class='tn-Powered-by-XIUMI' style='box-sizing: border-box;'/>
        </section>
        <section style='width: 0px; height: 0px; clear: both;'></section>
    </section>";
            #endregion

            StringBuilder sb = new StringBuilder();
            foreach (DataRow item in tb1.Rows)
            {
                sb.Append(string.Format(tempStr1, item["SecuAbbr"] + "(" + item["SecuCode"] + ")", star[Convert.ToInt32(item["Star"])], item["Contents"]));
            }

            string str1 = string.Format("{0}家上市公司披露{1}条公告，我们从中筛选出{1}条，供您参考。", tb2.Rows[0]["CompanyRecords"], tb2.Rows[0]["SumNewsRecords"], tb2.Rows.Count);
            sumStr = sumStr.Replace("{[template2]}", sb.ToString()).Replace("{[template1]}", str1);
            SaveToDb("lihao", "【重大利好" + System.DateTime.Now.ToString("MMdd") + "】", str1, sumStr, "http://static.emoney.cn/webupload/JiaQunTwoCode/2015/11/a2c817ba-fa06-4a24-8653-31b15ef8176e.jpg");
            return "1";
        }
        /// <summary>
        /// 将生成的内容保存至数据库
        /// </summary>
        /// <returns></returns>
        private bool SaveToDb(string typeName, string title, string summary, string content, string imgUrl = "")
        {
            try
            {
                NewsReportfbEntity news = new NewsReportfbEntity();
                news.Author = "autoCreater";
                news.Content = content;
                news.createtime = System.DateTime.Now;
                news.ImageUrl = imgUrl;
                news.isvalid = 1;
                news.keyword = typeName;
                news.MemuType = typeName;
                news.mtitle = title;
                news.Nature = "";
                news.newguid = System.Guid.NewGuid().ToString();
                news.PubMethod = "好股";
                news.PubStatus = 1;
                news.remark = "";
                news.title = title;
                news.Summary = summary;
                news.updatetime = System.DateTime.Now;
                NewsReportManager.UpdateNewsReportfb(news);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}
